//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.kingyea.esb.monitor.sampler;

import com.google.common.base.Preconditions;
import com.kingyea.esb.monitor.metricvalue.MetricValue;
import com.kingyea.esb.monitor.monitorsource.MonitorSource;
import com.kingyea.esb.monitor.monitorsource.MonitorSourceManager;
import com.kingyea.esb.monitor.monitorsource.SamplerMonitorSource;
import com.kingyea.esb.monitor.monitorsource.matcher.MonitorSourceMatcher;
import com.kingyea.esb.monitor.processor.MetricProcessor;
import com.kingyea.esb.monitor.quartz.ScheduleManager;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.triggers.CronTriggerImpl;
import org.slf4j.LoggerFactory;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

public class SchedulerSamplerExBean implements Task {
    private String cronExpression;
    private MonitorSourceMatcher monitorSourceMatcher;
    private MetricProcessor processor;
    private String jobGroup = "MONITOR-JOB-EX";
    private String jobName;
    private String triggerGroup = "MONITOR-TRIGGER-EX";
    private String triggerName;

    public SchedulerSamplerExBean() {
    }

    public void start() throws Exception {
        Preconditions.checkNotNull(this.monitorSourceMatcher, "monitorSourceMatcher不能为空");
        this.jobName = "Monitor_" + UUID.randomUUID().toString().replaceAll("-", "");
        this.triggerName = "Trigger_" + UUID.randomUUID().toString().replaceAll("-", "");
        JobDetail jobDetail = new JobDetailImpl(this.jobName, this.jobGroup, TaskBeanInvokeJob.class);// 这里不知道为什么要new一个接口：原new JobDetail
        jobDetail.getJobDataMap().put("task", this);
        CronTrigger cronTrigger = new CronTriggerImpl(this.triggerName, this.triggerGroup, this.cronExpression);// 这里不知道为什么要new一个接口，原new CronTrigger
//        ScheduleManager.getInstance().scheduleJob(jobDetail, cronTrigger);// 不知道什么版本问题，直接去掉 IncompatibleClassChangeError: Found interface org.quartz.JobExecutionContext, but class was expected
        LoggerFactory.getLogger(SchedulerSamplerExBean.class).info(this.jobName+" 没有执行，报错！！！注释掉了！");
    }

    public void stop() throws Exception {
        ScheduleManager.getInstance().pauseJob(this.jobGroup, this.jobName);
        ScheduleManager.getInstance().deleteJob(this.jobGroup, this.jobName);
    }

    public void execute() {
        Set<MonitorSource> monitorSources = MonitorSourceManager.getInstance().getMonitorSource(this.monitorSourceMatcher);
        if (monitorSources != null) {
            Iterator i$ = monitorSources.iterator();

            while(i$.hasNext()) {
                MonitorSource m = (MonitorSource)i$.next();
                if (m instanceof SamplerMonitorSource) {
                    SamplerMonitorSource samplerMonitorSource = (SamplerMonitorSource)m;
                    MetricValue metricValue = samplerMonitorSource.sample((Map)null);
                    if (this.processor != null) {
                        this.processor.process(metricValue);
                    }
                }
            }
        }

    }

    public String getCronExpression() {
        return this.cronExpression;
    }

    public void setCronExpression(String cronExpression) {
        this.cronExpression = cronExpression;
    }

    public MonitorSourceMatcher getMonitorSourceMatcher() {
        return this.monitorSourceMatcher;
    }

    public void setMonitorSourceMatcher(MonitorSourceMatcher monitorSourceMatcher) {
        this.monitorSourceMatcher = monitorSourceMatcher;
    }

    public MetricProcessor getProcessor() {
        return this.processor;
    }

    public void setProcessor(MetricProcessor processor) {
        this.processor = processor;
    }
}
